home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cocktail / puma.lha / puma / src / puma.rex < prev    next >
OS/2 REXX Batch file  |  1992-09-25  |  11KB  |  420 lines

  1. /* Ich, Doktor Josef Grosch, Informatiker, 20.3.1989 */
  2.  
  3. EXPORT  {
  4. FROM StringMem    IMPORT tStringRef;
  5. FROM Idents    IMPORT tIdent    ;
  6. FROM Texts    IMPORT tText    ;
  7. FROM Positions    IMPORT tPosition;
  8.  
  9. TYPE
  10. yyIdent = RECORD Ident: tIdent; END;
  11. yyOperator = RECORD Ident: tIdent; END;
  12. yyIncOperator = RECORD Ident: tIdent; END;
  13. yyTargetBlock = RECORD Text: tText; END;
  14. yyString = RECORD StringRef: tStringRef; END;
  15. yyNumber = RECORD StringRef: tStringRef; END;
  16. yyTargetCode = RECORD StringRef: tStringRef; END;
  17. yyWhiteSpace = RECORD StringRef: tStringRef; END;
  18. (* '::' *) yy9 = RECORD StringRef: tStringRef; END;
  19.  
  20. tScanAttribute = RECORD
  21. Position: tPosition;
  22. CASE : SHORTCARD OF
  23. | 1: Ident: yyIdent;
  24. | 2: Operator: yyOperator;
  25. | 3: IncOperator: yyIncOperator;
  26. | 4: TargetBlock: yyTargetBlock;
  27. | 5: String: yyString;
  28. | 6: Number: yyNumber;
  29. | 7: TargetCode: yyTargetCode;
  30. | 8: WhiteSpace: yyWhiteSpace;
  31. | 9: (* '::' *) yy9: yy9;
  32. END; END;
  33.  
  34. PROCEDURE ErrorAttribute (Token: INTEGER; VAR pAttribute: tScanAttribute);
  35.  
  36.  
  37. PROCEDURE Error        (Text: ARRAY OF CHAR; Position: tPosition);
  38. PROCEDURE ErrorI    (Text: ARRAY OF CHAR; Position: tPosition; Ident: tIdent);
  39. PROCEDURE Warning    (Text: ARRAY OF CHAR; Position: tPosition);
  40. PROCEDURE WarningI    (Text: ARRAY OF CHAR; Position: tPosition; Ident: tIdent);
  41. }
  42.  
  43. GLOBAL  {
  44. FROM SYSTEM    IMPORT ADR;
  45. FROM StringMem    IMPORT PutString;
  46. FROM Strings    IMPORT tString, Concatenate, Char, SubString,
  47.             AssignEmpty, Length, WriteL;
  48. FROM Idents    IMPORT tIdent, MakeIdent, NoIdent, GetStringRef;
  49. FROM Texts    IMPORT MakeText, Append;
  50. FROM Sets    IMPORT IsElement;
  51. FROM Tree    IMPORT Options, ErrorCount;
  52. FROM Positions    IMPORT tPosition;
  53.  
  54. IMPORT Errors;
  55.  
  56. VAR NestingLevel: INTEGER; Position, StringPos: tPosition;
  57.  
  58. PROCEDURE ErrorAttribute (Token: INTEGER; VAR pAttribute: tScanAttribute);
  59. BEGIN
  60.  pAttribute.Position := Attribute.Position;
  61.  CASE Token OF
  62.  | (* Ident *) 1: 
  63.  pAttribute.Ident.Ident    := NoIdent    ;
  64.  ;
  65.  | (* Operator *) 2: 
  66.  pAttribute.Operator.Ident    := NoIdent    ;
  67.  ;
  68.  | (* IncOperator *) 3: 
  69.  pAttribute.IncOperator.Ident    := NoIdent    ;
  70.  ;
  71.  | (* TargetBlock *) 4: 
  72.  MakeText (pAttribute.TargetBlock.Text); ;
  73.  ;
  74.  | (* String *) 5: 
  75.  pAttribute.String.StringRef    := GetStringRef (NoIdent);
  76.  ;
  77.  | (* Number *) 6: 
  78.  pAttribute.Number.StringRef    := GetStringRef (NoIdent);
  79.  ;
  80.  | (* TargetCode *) 7: 
  81.  pAttribute.TargetCode.StringRef    := GetStringRef (NoIdent);
  82.  ;
  83.  | (* WhiteSpace *) 8: 
  84.  pAttribute.WhiteSpace.StringRef    := GetStringRef (NoIdent);
  85.  ;
  86.  | (* '::' *) 9: 
  87.  pAttribute.yy9.StringRef    := GetStringRef (NoIdent);
  88.  ;
  89.  ELSE
  90.  END;
  91. END ErrorAttribute;
  92.  
  93.  
  94. PROCEDURE Error (Text: ARRAY OF CHAR; Position: tPosition);
  95.    BEGIN
  96.       Errors.Message (Text, Errors.Error, Position);
  97.       INC (ErrorCount);
  98.    END Error;
  99.  
  100. PROCEDURE ErrorI (Text: ARRAY OF CHAR; Position: tPosition; Ident: tIdent);
  101.    BEGIN
  102.       Errors.MessageI (Text, Errors.Error, Position, Errors.Ident, ADR (Ident));
  103.       INC (ErrorCount);
  104.    END ErrorI;
  105.  
  106. PROCEDURE Warning (Text: ARRAY OF CHAR; Position: tPosition);
  107.    BEGIN
  108.       IF NOT IsElement (ORD ('s'), Options) THEN
  109.      Errors.Message (Text, Errors.Warning, Position);
  110.       END;
  111.    END Warning;
  112.  
  113. PROCEDURE WarningI (Text: ARRAY OF CHAR; Position: tPosition; Ident: tIdent);
  114.    BEGIN
  115.       IF NOT IsElement (ORD ('s'), Options) THEN
  116.      Errors.MessageI (Text, Errors.Warning, Position, Errors.Ident, ADR (Ident));
  117.       END;
  118.    END WarningI;
  119. }
  120.  
  121. LOCAL    { VAR Word, String, TargetCode: tString; }
  122.  
  123. BEGIN    { NestingLevel := 0; }
  124.  
  125. DEFAULT    {
  126.    GetWord (Word);
  127.    Errors.MessageI ("illegal character", Errors.Error, Attribute.Position, Errors.String, ADR (Word));
  128. }
  129.  
  130. EOF     {
  131.    CASE yyStartState OF
  132.    | comment    : Error ("unclosed comment", Position);
  133.    | expr    ,
  134.      targetcode    : Error ("unclosed target code", Position);
  135.    | CStr1, CStr2,
  136.      Str1, Str2    : Error ("unclosed string", StringPos);
  137.    ELSE
  138.    END;
  139. }
  140.  
  141. DEFINE  letter  =   {A-Z a-z _}    .
  142.         digit   =   {0-9}    .
  143.     CmtCh   = - {*\t\n}    .
  144.     StrCh1    = - {'\t\n}    .
  145.     StrCh2    = - {"\t\n}    .
  146.     CStrCh1    = - {'\t\n\\}    .
  147.     CStrCh2    = - {"\t\n\\}    .
  148.     code    = - {\\\t\n{\}'"} .
  149.     anyExpr    = - {\\\t\n{\}'":\ a-zA-Z} .
  150.  
  151. START    comment, Str1, Str2, CStr1, CStr2, targetcode, expr
  152.  
  153. RULE
  154.  
  155. #targetcode#    "{"    : {
  156.                IF NestingLevel = 0 THEN
  157.                   MakeText (Attribute.TargetBlock.Text);
  158.                   AssignEmpty (TargetCode);
  159.                   Position := Attribute.Position;
  160.                ELSE
  161.                   GetWord (Word);
  162.                   Concatenate (TargetCode, Word);
  163.                END;
  164.                INC (NestingLevel);
  165.             }
  166.  
  167. #targetcode#    "}"    :- {
  168.                DEC (NestingLevel);
  169.                IF NestingLevel = 0 THEN
  170.                   yyStart (STD);
  171.                   Append (Attribute.TargetBlock.Text, TargetCode);
  172.                   Attribute.Position := Position;
  173.                   RETURN 4;
  174.                ELSE
  175.                   GetWord (Word);
  176.                   Concatenate (TargetCode, Word);
  177.                END;
  178.             }
  179.  
  180. #targetcode#    code +    :- {
  181.                IF NestingLevel > 0 THEN
  182.                   GetWord (Word);
  183.                   Concatenate (TargetCode, Word);
  184.                END;
  185.             }
  186.  
  187. #targetcode#    \t    :- {
  188.                IF NestingLevel > 0 THEN
  189.                   Strings.Append (TargetCode, 11C);
  190.                END;
  191.                yyTab;
  192.             }
  193.  
  194. #targetcode#    \n    :- {
  195.                IF NestingLevel > 0 THEN
  196.                   Append (Attribute.TargetBlock.Text, TargetCode);
  197.                   AssignEmpty (TargetCode);
  198.                END;
  199.                yyEol (0);
  200.             }
  201.  
  202. #targetcode#    \\ ANY    :- {
  203.                IF NestingLevel > 0 THEN
  204.                   GetWord (Word);
  205.                   Strings.Append (TargetCode, Char (Word, 2));
  206.                END;
  207.             }
  208.  
  209. #targetcode#    \\    :- {
  210.                IF NestingLevel > 0 THEN
  211.                   Strings.Append (TargetCode, '\');
  212.                END;
  213.             }
  214.  
  215. #STD, expr# "/*"    :  {yyStart (comment); Position := Attribute.Position;}
  216. #comment# "*/"        :- {yyPrevious;}
  217. #comment# "*" | CmtCh +    :- {}
  218.  
  219. #STD# \f | \r        :- {}
  220.  
  221. #STD# (digit + "." digit * | digit * "." digit +) ({Ee} {+\-} ? digit +) ? | digit +
  222.             :  {GetWord (Word);
  223.                         Attribute.Number.StringRef := PutString (Word);
  224.                 RETURN 6;}
  225.  
  226. #STD, expr, targetcode# ' :{GetWord (String);
  227.                 StringPos := Attribute.Position;
  228.                 IF IsElement (ORD ('c'), Options)
  229.                 THEN yyStart (CStr1);
  230.                 ELSE yyStart (Str1);
  231.                 END;}
  232.  
  233. #STD, expr, targetcode# \":{GetWord (String);
  234.                 StringPos := Attribute.Position;
  235.                 IF IsElement (ORD ('c'), Options)
  236.                 THEN yyStart (CStr2);
  237.                 ELSE yyStart (Str2);
  238.                 END;}
  239.  
  240. #Str1#    StrCh1 +    ,
  241. #Str2#    StrCh2 +    ,
  242. #CStr1#    CStrCh1 + | \\ ANY ? ,
  243. #CStr2#    CStrCh2 + | \\ ANY ? :- {GetWord (Word); Concatenate (String, Word);}
  244.  
  245. #CStr1#    \\ \n        ,
  246. #CStr2#    \\ \n        :- {GetWord (Word); Concatenate (String, Word); yyEol (0);}
  247.  
  248. #Str1, CStr1# '        ,
  249. #Str2, CStr2# \"    :- {Strings.Append (String, Char (String, 1));
  250.                 yyPrevious;
  251.                 IF yyStartState = targetcode THEN
  252.                    Concatenate (TargetCode, String);
  253.                 ELSE
  254.                    Attribute.String.StringRef := PutString (String);
  255.                    RETURN 5;
  256.                 END;}
  257.  
  258. #Str1, Str2, CStr1, CStr2# \t :- {Strings.Append (String, 11C); yyTab;}
  259.  
  260. #Str1, Str2, CStr1, CStr2# \n :- {Error ("unclosed string", Attribute.Position);
  261.                 Strings.Append (String, Char (String, 1));
  262.                 yyEol (0); yyPrevious;
  263.                 IF yyStartState = targetcode THEN
  264.                    Concatenate (TargetCode, String);
  265.                 ELSE
  266.                    Attribute.String.StringRef := PutString (String);
  267.                    RETURN 5;
  268.                 END;}
  269.  
  270. #STD# "::"        : {RETURN 9            ;}
  271.  
  272. #STD# "{"        : {IF NestingLevel = 0 THEN Position := Attribute.Position; END;
  273.                yyStart (expr); INC (NestingLevel); RETURN 34;}
  274.  
  275. #expr# anyExpr *    : {GetWord (Word);
  276.                Attribute.TargetCode.StringRef := PutString (Word);
  277.                RETURN 7        ;}
  278.  
  279. #expr# "{"        : {INC (NestingLevel);
  280.                GetWord (Word);
  281.                Attribute.TargetCode.StringRef := PutString (Word);
  282.                RETURN 7        ;}
  283.  
  284. #expr# "}"        : {DEC (NestingLevel);
  285.                IF NestingLevel = 0 THEN
  286.                   yyStart (STD);
  287.                   RETURN 35;
  288.                ELSE
  289.                   GetWord (Word);
  290.                   Attribute.TargetCode.StringRef := PutString (Word);
  291.                   RETURN 7;
  292.                END                ;}
  293.  
  294. #expr# ":"        : {GetWord (Word);
  295.                Attribute.TargetCode.StringRef := PutString (Word);
  296.                RETURN 7        ;}
  297.  
  298. #expr# "::"        : {GetWord (Word);
  299.                Attribute.yy9.StringRef := PutString (Word);
  300.                RETURN 9            ;}
  301.  
  302. #expr# " " +        : {GetWord (Word);
  303.                Attribute.WhiteSpace.StringRef := PutString (Word);
  304.                RETURN 8        ;}
  305.  
  306. #expr# \n        : {GetWord (Word);
  307.                Attribute.WhiteSpace.StringRef := PutString (Word);
  308.                yyEol (0);
  309.                RETURN 8        ;}
  310.  
  311. #expr# \t        : {GetWord (Word);
  312.                Attribute.WhiteSpace.StringRef := PutString (Word);
  313.                yyTab;
  314.                RETURN 8        ;}
  315.  
  316. #expr# \\ ANY        : {GetWord (Word);
  317.                SubString (Word, 2, 2, String);
  318.                Attribute.TargetCode.StringRef := PutString (String);
  319.                RETURN 7        ;}
  320.  
  321. #expr# \\        : {GetWord (Word);
  322.                Attribute.TargetCode.StringRef := PutString (Word);
  323.                RETURN 7        ;}
  324.  
  325. #STD# BEGIN        : {yyStart (targetcode); RETURN 19    ;}
  326. #STD# CLOSE        : {yyStart (targetcode); RETURN 20    ;}
  327. #STD# EXPORT        : {yyStart (targetcode); RETURN 16;}
  328. #STD# GLOBAL        : {yyStart (targetcode); RETURN 18;}
  329. #STD# IMPORT        : {yyStart (targetcode); RETURN 17;}
  330. #STD# LOCAL        : {yyStart (targetcode); RETURN 30    ;}
  331.  
  332. #STD# 
  333.   "!"
  334. | "!="
  335. | "#"
  336. | "%"
  337. | "&"
  338. | "&&"
  339. | "*"
  340. | "+"
  341. | "-"
  342. | "/"
  343. | "<"
  344. | "<<"
  345. | "<="
  346. | "<>"
  347. | "="
  348. | "=="
  349. | ">"
  350. | ">="
  351. | ">>"
  352. | "|"
  353. | "||"
  354. | "~"
  355. | AND
  356. | DIV
  357. | IN
  358. | MOD
  359. | \NOT
  360. | OR
  361.             : {GetWord (Word);
  362.                        Attribute.Operator.Ident := MakeIdent (Word);
  363.                RETURN 2        ;}
  364.  
  365. #STD# "++" | "--"    : {GetWord (Word);
  366.                        Attribute.IncOperator.Ident := MakeIdent (Word);
  367.                RETURN 3        ;}
  368.  
  369. #STD# \\ - {\ \t\n} +    : {GetWord (Word);
  370.                SubString (Word, 2, Length (Word), String);
  371.                        Attribute.Operator.Ident := MakeIdent (String);
  372.                RETURN 2        ;}
  373.  
  374.  #STD#TRAFO    : { RETURN 10; }
  375.  #STD#TREE    : { RETURN 11; }
  376.  #STD#\,    : { RETURN 12; }
  377.  #STD#PUBLIC    : { RETURN 13; }
  378.  #STD#EXTERN    : { RETURN 14; }
  379.  #STD#\;    : { RETURN 15; }
  380.  #STD#EXPORT    : { RETURN 16; }
  381.  #STD#IMPORT    : { RETURN 17; }
  382.  #STD#GLOBAL    : { RETURN 18; }
  383.  #STD#BEGIN    : { RETURN 19; }
  384.  #STD#CLOSE    : { RETURN 20; }
  385.  #STD#PROCEDURE    : { RETURN 21; }
  386.  #STD#\(    : { RETURN 22; }
  387.  #STD#REF    : { RETURN 23; }
  388.  #STD#\:    : { RETURN 24; }
  389.  #STD#\.    : { RETURN 25; }
  390.  #STD#\[    : { RETURN 26; }
  391.  #STD#\]    : { RETURN 27; }
  392.  #STD#\=\>    : { RETURN 28; }
  393.  #STD#\)    : { RETURN 29; }
  394.  #STD#LOCAL    : { RETURN 30; }
  395.  #STD#\.\.    : { RETURN 31; }
  396.  #STD#NIL    : { RETURN 32; }
  397.  #STD#_    : { RETURN 33; }
  398.  #STD#\{    : { RETURN 34; }
  399.  #STD#\}    : { RETURN 35; }
  400.  #STD#\-\>    : { RETURN 36; }
  401.  #STD#\^    : { RETURN 37; }
  402.  #STD#\:\>    : { RETURN 38; }
  403.  #STD#\:\=    : { RETURN 39; }
  404.  #STD#\?    : { RETURN 40; }
  405.  #STD#REJECT    : { RETURN 41; }
  406.  #STD#FAIL    : { RETURN 42; }
  407.  #STD#NL    : { RETURN 43; }
  408.  #STD#RETURN    : { RETURN 44; }
  409.  #STD#FUNCTION    : { RETURN 45; }
  410.  #STD#PREDICATE    : { RETURN 46; }
  411.  
  412.  
  413. #STD# "..."        : {RETURN 31            ;}
  414. #STD# ":-"        : {RETURN 40            ;}
  415.  
  416. #STD, expr# letter (letter | digit) *
  417.             : {GetWord (Word);
  418.                        Attribute.Ident.Ident := MakeIdent (Word);
  419.                RETURN 1            ;}
  420.